﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using THEDARKKNIGHT.UI;
using THEDARKKNIGHT.FileSystem;
using System;
using THEDARKKNIGHT.ConstDefine;
using THEDARKKNIGHT.EventSystem;
using System.IO;
using UnityEngine.UI;

public class VideoListWindows : BBaseWindows
{
    private GameObject ReUIRoot;

    private GameObject VideoItem;

    private Transform VideoListParent;

    private Dictionary<string, VideoInfo> VideoDic = new Dictionary<string, VideoInfo>();

    public VideoListWindows(string windowsName) {
        this.WindowsAlias = windowsName;
    }



    public override void AddListener()
    {
    }

    public override void DataInit()
    {
        ReUIRoot = Resources.Load<GameObject>("UIPrefabs/VideoListPanel");
        VideoItem = Resources.Load<GameObject>("UIPrefabs/VideoItem");
        FileInfo[] files = BFileSystem.Instance().GetFiles(BFameWorkPathDefine.BVideoPath, "*.mp4", System.IO.SearchOption.TopDirectoryOnly);
        for (int i = 0; i < files.Length; i++)
        {
            VideoInfo videoInfo = new VideoInfo();
            videoInfo.VideoName = files[i].Name.Split('.')[0];
            videoInfo.URL = files[i].FullName;
            VideoDic.Add(files[i].Name, videoInfo);
        }
    }

    public override void DestoryWindows()
    {
        ReUIRoot = null;
    }

    public override bool GetWindowsMsg(int windowsID, string windowsAlias, object data)
    {
        return true;
    }

    public override void Hide()
    {
    }

    public override void RemoveListener()
    {
    }

    public override void Show()
    {
    }

    public override Transform UIInit(Transform UIRoot)
    {
        GameObject videoUIRoot = GameObject.Instantiate(ReUIRoot, UIRoot);
        VideoListParent = videoUIRoot.transform.Find("VideoList/Viewport/Content");
        //GameObject.Instantiate(VideoItem, VideoListParent);
        foreach (KeyValuePair<string, VideoInfo> item in VideoDic) {
            byte[] data = BFileSystem.Instance().ReadFileFromDisk(BFameWorkPathDefine.BVideoThumbNailPath, item.Value.VideoName+".jpg");
            Texture2D texture = new Texture2D(130, 72);
            texture.LoadImage(data);
            GameObject videoItem = GameObject.Instantiate(VideoItem, VideoListParent);
            Image thumbNail = videoItem.transform.Find("ThumbNail").GetComponent<Image>();
            Text descript = videoItem.transform.Find("Descript").GetComponent<Text>();
            Button btn = videoItem.transform.Find("Background").GetComponent<Button>();
            thumbNail.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
            btn.onClick.AddListener(() =>
            {
                VideoStatus status = new VideoStatus();
                status.IsPlay = true;
                status.VideoName = item.Value.VideoName;
                PostMsgToWindows("PlayWindows", status);
            });
            descript.text = item.Value.VideoName;
        }
        return videoUIRoot.transform;
    }

    private class VideoInfo {

        public string VideoName;

        public string URL;

    }
}
